using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// PX_Course
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class PX_CourseController : CoreApiController
    {
        private readonly PX_CourseService _service;
        private readonly CoreDBContext _coreDBContext;
        /// <summary>
        /// PX_Course
        ///</summary>
        public PX_CourseController(PX_CourseService service, CoreDBContext coreDBContext)
        {
            _service = service;
            _coreDBContext = coreDBContext;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(PX_CourseController) + "_" + nameof(PX_CourseController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<PX_CourseDto>
                pagingQueryModel)
        {
            IPageModel<PX_CourseDto>
                result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Id,
                a.CoursewareName,//课程名称
                //a.ClassificationofCourseware,//课程分类ID
                a.PX_CourseClass.Name,//分类名称
                a.PX_CourseTeacher.TeacherText,//培训老师
                //a.Learningtime,//学习时长
                //a.Encryptionornot,//是否加密
                //a.CourseAttachment,//培训课件附件
                //a.Reamrk,//备注
                //a.CreateFactory,//隶属工厂
                //a.CreateDept,//
                //a.UpdateDept,//
                a.CreateTime,//创建时间
            }
            ).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }


        

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(PX_CourseController) + "_" + nameof(PX_CourseController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(PX_CourseDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(PX_CourseController) + "_" + nameof(PX_CourseController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var retObj = new
            {
                model.CoursewareName,//课程名称
                //model.ClassificationofCourseware,//课程分类ID
                //model.Learningtime,//学习时长
                //model.Encryptionornot,//是否加密
                model.CourseAttachment,//培训课件附件
                //model.Reamrk,//备注
                //model.CreateFactory,//隶属工厂
                //model.CreateDept,//
                //model.UpdateDept,//
            };
            return this.JsonSuccess(retObj);
        }


        /// <summary>
        /// 获取培训_课程分类
        /// </summary>
        /// <returns></returns>

        [HttpGet("GetCourseClassList")]
        public async Task<IActionResult> GetCourseClassList()
        {
            var list = await _coreDBContext.PX_CourseClass.ToListAsync();
            return this.JsonSuccess(list?.Select(a => new
            {
                a.Id,//ID
                a.PId,//上级分类ID
                a.Name//分类名称
            }));
        }



        #endregion


    }
}
